<template>
  <div style="position: relative;">
    <el-form :inline="true" size="mini" :model="formInline" label-position="right">
      <el-form-item label="工作流程名称">
        <el-input class="filter-item" v-model="formInline.flowName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="所属业务">
        <el-cascader v-model="businesstypeId"
          ref="ref_business"
          :options="businesstypeTree"
          :props="businesstProps"
          :show-all-levels="false"
          clearable
          @change="businessChange">
        </el-cascader>
      </el-form-item>
      <el-form-item label="所属阶段">
        <el-select v-model="formInline.businessStage" placeholder="请选择">
          <el-option label="商机阶段" value="1"></el-option>
          <el-option label="立项阶段" value="2"></el-option>
          <el-option label="合同阶段" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="启用状态">
        <el-select v-model="formInline.enabled" placeholder="请选择">
          <el-option label="启用" value="1"></el-option>
          <el-option label="停用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="onSubmit">搜索</el-button>
        <el-button type="warning" class="el-button--reset" icon="el-icon-refresh-right" @click="resetFromdata" size="mini"></el-button>
        <el-button type="primary" size="mini" @click="addFromdata">创建工作流程</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="24">
        <el-table :data="tableData" size="mini" style="width: 100%;" header-cell-class-name="table-header-gray" v-loading="isShowloading">
          <el-table-column type="index" align="center" width="55" label="序号"></el-table-column>
          <el-table-column prop="flowName" align="center" label="工作流程名称"></el-table-column>
          <el-table-column prop="businessTypeName" align="center" label="所属业务"></el-table-column>
          <el-table-column prop="businessStageName" align="center" label="所属阶段"></el-table-column>
          <el-table-column prop="createUserName" align="center" label="创建人"></el-table-column>
          <el-table-column prop="createTime" align="center" label="创建时间"></el-table-column>
          <el-table-column prop="enabledName" align="center" label="启用状态"></el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="handleEdit(scope.row)">修改</el-button>
              <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="end" style="margin-top: 10px;">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNo"
            :page-size="page_size"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            :total="total"></el-pagination>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { Workflow, Businesstype } from '@/api/business.js';
import Workflowadd from './WorkflowAdd.vue';
export default {
  name: 'WorkflowList',
  data () {
    return {
      editDialogVisible: false,
      businesstypeTree: [],
      businesstypeId: [],
      businesstProps: {
        value: 'id',
        label: 'businessName',
        children: 'children',
        checkStrictly: true
      },
      formInline: {
        flowName: null,
        businessType: null,
        businessStage: null,
        enabled: null
      },
      tableData: [],
      total: 0,
      page_size: 10,
      pageNo: 1,
      isShowloading: false
    }
  },
  created () {
    this.loadWorkflowWidgetData()
    this.loadbusinesstypeTree()
  },
  computed: {},
  methods: {
    /**
     * 业务类型信息表树形列表
     */
    loadbusinesstypeTree () {
      return new Promise((resolve, reject) => {
        Businesstype.tree(this).then(res => {
          this.businesstypeTree = res.data;
        }).catch(e => {
          reject(e);
        });
      });
    },
    businessChange () {
      let info = this.businesstypeId
      let length = info.length - 1
      this.formInline.businessType = info[length]
      this.$refs['ref_business'].dropDownVisible = false
    },
    /**
     * 工作流程
     */
    loadWorkflowWidgetData () {
      let params = {
        ...this.formInline,
        size: this.page_size,
        current: this.pageNo
      }
      return new Promise((resolve, reject) => {
        Workflow.list(this, params).then(res => {
          this.tableData = res.data.records
          this.total = res.data.total
        }).catch(e => {
          reject(e);
        });
      });
    },
    // 创建工作流程
    addFromdata () {
      let params = {};
      this.$dialog.show('新建工作流程', Workflowadd, {
        area: '900px'
      }, params).then(res => {
        this.loadWorkflowWidgetData();
      }).catch(e => {});
    },
    // 搜索
    onSubmit () {
      this.pageNo = 1
      this.loadWorkflowWidgetData()
    },
    // 重置
    resetFromdata () {
      this.formInline = {
        flowName: null,
        businessType: null,
        businessStage: null,
        enabled: null
      }
      this.loadWorkflowWidgetData()
    },
    // 分页导航
    handleCurrentChange (val) {
      this.pageNo = val
      this.loadWorkflowWidgetData()
    },
    handleSizeChange (val) {
      this.page_size = val
      this.loadWorkflowWidgetData()
    },
    currentChange (val) {
      this.pageNo = val
      this.loadKnowledgetypeWidgetData()
    },
    // 修改
    handleEdit (row) {
      this.$dialog.show('编辑工作流程', Workflowadd, {
        area: '800px'
      }, {
        workflowId: row.id
      }).then(res => {
        this.loadWorkflowWidgetData()
      }).catch(e => {
      });
    },
    // 删除
    handleDelete (row) {
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          id: row.id
        }
        return new Promise((resolve, reject) => {
          Workflow.delete(this, params).then(res => {
            this.loadWorkflowWidgetData()
          }).catch(e => {
            reject(e);
          });
        });
      })
    }
  }
}
</script>
<style scoped>
  .el-cascader-menu {
    height: 200px;
  }
</style>
